﻿using BW.ME.Exam.ErrorCode;
using BW.ME.Exam.Write.Api.Applications.Commands.ClassCreateCommand;
using BW.ME.Exam.Write.Api.Applications.Commands.GClassCommands;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BW.ME.Exam.Write.Api.Controllers
{
    /// <summary>
    /// Api-写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ManagementController : ControllerBase
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public ManagementController(IMediator mediator,ILogger<ManagementController> logger)
        {
            Mediator = mediator;
            Logger = logger;
        }
        /// <summary>
        /// 中介者
        /// </summary>
        public IMediator Mediator { get; }
        /// <summary>
        /// 日志
        /// </summary>
        public ILogger<ManagementController> Logger { get; }


        /// <summary>
        /// 添加应用
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<ApiResult<object>> CreateInstance(ClassCreateCommand command)
        {
            //异常处理
            try
            {
                Logger.LogInformation("调用了一下添加应用方法");
                return await Mediator.Send(command);
            }
            catch (Exception exp)
            {
                Logger.LogError("调用添加方法报错" + exp.Message);
                throw;
            }
        }



        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<object>> UpdateApp(FormUpdateCommand command)
        {
            try
            {
                Logger.LogInformation("调用了一下审批用方法");
                return await Mediator.Send(command);
            }
            catch (Exception exp)
            {
                Logger.LogError("调用审批方法报错" + exp.Message);
                throw;
            }
        }
    }
}
